<?php

namespace app\resources\service;

use app\resources\model\Nodes;
use app\resources\model\UserFrp;
use app\resources\model\NodesPort;
use app\resources\model\WebSocket;

/**
 * 服务基类
 */
class FrpsService extends BaseService
{

    /**
     * 创建frp
     * @param array $param 
     * @return void
     */
    public function createFrps(array $param)
    {
        $node = Nodes::find( $param['nid'] );
        if(!$node || !$node->is_line){
            throw new \think\Exception('节点不在线！');
        }
        $frp = UserFrp::create($param);
        NodesPort::create(['nid'=>$frp->nid,'port'=>$frp->server_port]);
        NodesPort::create(['nid'=>$frp->nid,'port'=>$frp->server_port+1]);
        $token = UserFrp::getSecretKey();
        try {
            // 发送cmd
            $id = $frp->id;
            $server = env('APP_HOST');
            $result = WebSocket::pushNodeCmd([
                'uid'=>$node->host,
                'msg'=> [
                    'type'=>'cmd:host',
                    // 'body'=><<<EOF 路由没用
                    //     curl -sSL "$server/system/node/get-frps?id=$id&token=$token" | sh
                    //     EOF
                ]
            ]);
            if($result==false){
                throw new \think\Exception('请求超时！');
            }
            if( ($result['out'][0] ?? '') != 'success'){
                throw new \think\Exception($result['out'][0]);
            }
            return $result;
        } catch (\Exception $e) {
            NodesPort::where(['nid'=>$frp->nid,'port'=>$frp->server_port])->delete();
            NodesPort::where(['nid'=>$frp->nid,'port'=>$frp->server_port+1])->delete();
            $frp->delete();
            throw new \think\Exception($e->getMessage());
        }
    }

    /**
     * 删除frps
     * 
     * @param integer $fid 
     * @return void
     */
    public function deleteFrps(int $fid)
    {
        $frp = UserFrp::find( $fid );
        if(!$frp){
            throw new \think\Exception('frp不存在');
        }
        if(!$frp->node || !$frp->node?->is_line){
            throw new \think\Exception('节点不在线');
        }
        $parentDir = UserFrp::DIRECTORY;
        $fileName = $frp->fileName();
        $result = WebSocket::pushNodeCmd([
            'uid'=> $frp->node->host,
            'msg'=> [
                'type'=>'cmd:host',
                'body'=><<<EOF
                    rm -if $parentDir/$fileName
                    for line in $(ps aux|grep frp| grep -v grep | grep $fileName | awk '{print $2}')
                    do
                    done
                    EOF
            ]
        ]);
        if($result==false){
            throw new \think\Exception('请求超时！');
        }
        if(($result['out'][0] ?? '') != 'success'){
            throw new \think\Exception('删除失败！');
        }
        $frp->delete();
    }

    /**
     * 重启动frps
     *
     * @param integer $fid 
     * @return void
     */
    public function restartFrps(int $fid)
    {
        $frp = UserFrp::find( $fid );
        if(!$frp){
            throw new \think\Exception('frp不存在');
        }
        if(!$frp->node || !$frp->node?->is_line){
            throw new \think\Exception('节点不在线');
        }
        $parentDir = UserFrp::DIRECTORY;
        $fileName = $frp->fileName();
        $result = WebSocket::pushNodeCmd([
            'uid'=> $frp->node->host,
            'msg'=> [
                'type'=>'cmd:host',
                'body'=><<<EOF
                    nohup /usr/frps/frps -c $parentDir/$fileName >/dev/null 2>&1 &
                    EOF
            ]
        ]);
        if($result==false){
            throw new \think\Exception('请求超时！');
        }
        if(($result['out'][0] ?? '') != 'success'){
            throw new \think\Exception('重启失败！');
        }
    }

}
